package com.csh.szhm.activity;

import java.util.List;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.VolleyError;
import com.csh.szhm.R;
import com.csh.szhm.adapter.MyBaseAdapter;
import com.csh.szhm.custorm.likenetease.LikeNeteasePull2RefreshListView;
import com.csh.szhm.custorm.likenetease.LikeNeteasePull2RefreshListView.OnLoadMoreListener;
import com.csh.szhm.custorm.likenetease.LikeNeteasePull2RefreshListView.OnRefreshListener;
import com.csh.szhm.utils.DebugUntil;

public abstract class MyRefreshBaseActivity<T> extends MyBaseActivity<List<T>>
		implements OnRefreshListener, OnLoadMoreListener {
	protected int pageIndex = 1;
	protected int pageSize = 20;
	protected boolean isRefresh;
	protected MyBaseAdapter<T> mAdapter;
	protected LikeNeteasePull2RefreshListView mLikeListView;
	protected boolean mShowToast;

	@Override
	protected void init() {
		mLikeListView = (LikeNeteasePull2RefreshListView) findViewById(R.id.szhm_main_listview);
		mLikeListView.setOnRefreshListener(this);
		mLikeListView.setOnLoadListener(this);
		mLikeListView.setAutoLoadMore(false);
		mLikeListView.setCanLoadMore(false);
		mLikeListView.setCanRefresh(true);
		mLikeListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				 onMyItemClick(mAdapter.getItem(position - 1));
			}
		});
	}

	protected void onMyItemClick(T item) {

	}

	@Override
	public void success(List<T> data) {
		if (mLikeListView != null) {
			mLikeListView.stopCommplete();
		}
		if (isRefresh) {
			mAdapter.clearAll();
		}
		if (data != null) {
			List ls = (List) data;
			if (ls.size() > 0) {
				isRefresh = false;
				if (ls.size() < pageSize) {
					mLikeListView.setAutoLoadMore(false);
					mLikeListView.setCanLoadMore(false);
				} else {
					mLikeListView.setAutoLoadMore(true);
					mLikeListView.setCanLoadMore(true);
				}
				mAdapter.addData(ls);
			} else {
				showToast();
			}

		} else {
			showToast();
		}
		super.success(data);
	}

	private void showToast() {
		if (!mShowToast) {
			DebugUntil.createInstance().toastStr("无可用数据");
		}
	}

	@Override
	public void failed(VolleyError error) {
		pageIndex = pageIndex == 1 ? 1 : --pageIndex;
		if (mLikeListView != null) {
			mLikeListView.stopCommplete();
		}
		super.failed(error);
	}

	@Override
	public void onLoadMore() {

		isRefresh = false;
		pageIndex++;
		requestNet();
	}

	@Override
	public void onRefresh() {
		isRefresh = true;
		pageIndex = 1;
		requestNet();
	}

	protected String getCurrentPage() {
		return "&pageInfo.pageNo=" + pageIndex;
	}

	protected void stopOnLoad(int size) {
		if (size >= 10) {
			mLikeListView.setCanLoadMore(true);
			mLikeListView.setAutoLoadMore(true);
		} else {
			mLikeListView.setCanLoadMore(false);
			mLikeListView.setAutoLoadMore(false);
		}
		mLikeListView.stopCommplete();
	}

}
